<div class="layui-card-header layui-card">
    <span class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a><cite>银行账号记录</cite></a>
    </span>
</div>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">银行账号记录</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-card-body">
                    <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
                    </div>
                </div>
                <table id="bankList" lay-filter="bankList"></table>
            </div>
        </div>
    </div>
</div>
<script type="text/html" id="tpTitle">
    {{# if(d.isDefault == 1){  }}
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="detail" >详情</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit" >修改</a> 
        <a class="layui-btn layui-btn-xs cMor layui-btn-normal" lay-event="quxiao" >取消默认</a>
    {{#  } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-primary" lay-event="detail" >详情</a>
        <a class="layui-btn layui-btn-xs" lay-event="edit" >修改</a> 
        <a class="layui-btn layui-btn-xs morA layui-btn-normal" lay-event="moren" >设为默认</a>
    {{#  } }}
</script>
<script>
    layui.use(['table','util','admin','form'], function(){
        var table = layui.table
        ,$ = layui.$
        ,admin = layui.admin
            ,view =layui.view
            ,layer=layui.layer,
            form =layui.form
        ,element = layui.element;

        element.render('breadcrumb', 'breadcrumb');//渲染导航信息


        //监听工具条
        table.on('tool(bankList)', function(obj){ 
            var data = obj.data;
        
            if(obj.event === 'detail'){ //查看

                var creId =data.id;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/bank_account/get',
                    data: {
                        id: creId
                    },
                    success: function(res){
                        if(res.code == 0){
                            data=res.data;

                            admin.popup({
                                title: '银行详情'
                                , area: ['730px', '360px']
                                , id: 'LAY-popup-content-detail'
                                , success: function (layero, index) {
                                    view(this.id).render('sett/bank/detail').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');

                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        form.val("layuiadmin-app-form-list",{

                                            "id":data.id,
                                            "mchId":data.mchId,
                                            "name":data.name,
                                            "accountNo":res.data.accountNo,
                                            "remark":data.remark,
                                            "isDefault":data.isDefault==1?"默认":"不是默认",
                                            "accountType":data.accountType==1?"银行账号":data.accountType==2?"支付宝账号":data.accountType==3?"微信账号":'发生错误'

                                        })

                                        $('#createTime').val( layui.util.toDateString(res.data.createTime));
                                        $('#updateTime').val( layui.util.toDateString(res.data.updateTime));
                                        form.render();
                                    });
                                }
                            });

                        }else {
                            layer.alert(res.msg,{
                                title: '请求成功，但是状态不对'
                            })
                        }
                    }
                })


            } else if(obj.event === 'edit'){ //编辑

                var crid =data.id;
                var cracInfo =data.accountNo;
                admin.req({
                    type: 'get',
                    url: layui.setter.baseUrl + '/bank_account/get',
                    data: {
                        id: crid,
                        accountNo: cracInfo
                    },
                    error: function(err){
                        layer.alert(err);
                    },
                    success: function(res){
                        if(res.code == 0 ){

                            admin.popup({
                                title: '修改银行账号'
                                , area: ['535px', '320px']
                                , id: 'LAY-popup-content-view'
                                , success: function (layero, index) {
                                    view(this.id).render('sett/bank/update').done(function () {
                                        form.render(null, 'layuiadmin-app-form-list');

                                        var body =layer.getChildFrame('body',index);
                                        body.find('#email').val(data.email);
                                        form.val("layuiadmin-app-form-list",{

                                            "accountNo":data.accountNo,
                                            "remark":data.remark

                                        })
                                        if(res.data.accountType == 1){
                                            $('.bankC').prop('checked',true);

                                            $('.alipay').prop('disabled',true);
                                            $('.wxpay').prop('disabled',true);
                                        }else if(res.data.accountType == 2){
                                            $('.alipay').prop('checked',true);

                                            $('.bankC').prop('disabled',true);
                                            $('.wxpay').prop('disabled',true);

                                        }else if(res.data.accountType == 3){
                                            $('.wxpay').prop('checked',true);

                                            $('.alipay').prop('disabled',true);
                                            $('.bankC').prop('disabled',true);
                                        }


                                        form.render();

                                        form.on('submit(btnUP)', function(data){
                                            // 在这个回调函数里面写ajax提交
                                            admin.req({
                                                type: 'post',
                                                url: layui.setter.baseUrl + '/bank_account/update',
                                                // data: "params=" + JSON.stringify(data.field),
                                                data: {
                                                    accountType : data.field.accountType,
                                                    accountNo: data.field.accountNo,
                                                    remark: data.field.remark,
                                                    id:crid
                                                },
                                                success: function(res){
                                                    console.log(res);
                                                    if(res.code == 0){
                                                        /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                            //layer.closeAll();
                                                            //location.hash = '/sett/bank/';
                                                            table.reload('tableReload');
                                                            layer.closeAll();
                                                        })*/
                                                        layer.msg('保存成功', {
                                                            offset: '15px'
                                                            ,icon: 1
                                                            ,time: 1000
                                                        }, function(){
                                                            table.reload('tableReload');
                                                            layer.closeAll();
                                                        });
                                                    }else {
                                                       /* layer.alert(res.msg,{title: '保存失败'})*/
                                                        layer.msg('保存失败', {
                                                            offset: '15px'
                                                            ,icon: 1
                                                            ,time: 1000
                                                        });
                                                    }

                                                }
                                            })

                                        });
                                        form.render();

                                    });
                                }
                            });

                        }else {
                            layer.alert(res.msg,{title: '请求失败'})
                        }
                        form.render();
                    }

                })

            } else if(obj.event === 'quxiao'){ //取消默认
                layer.confirm('确认取消默认么', function(){
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/bank_account/default_cancel',
                        data: {id: data.id, access_token: layui.data(layui.setter.tableName).access_token},
                        error: function(err){layer.alert(err),{title:"请求失败"}},
                        success: function(res){
                            if(res.code == 0){
                                /*layer.alert(res.msg,{title:"取消默认成功"},function(index){
                                    //layer.close(index);
                                    //table.reload('bankList');
                                    table.reload('tableReload');
                                    layer.closeAll();
                                });*/
                                layer.msg('取消默认成功', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                }, function(){
                                    table.reload('tableReload');
                                    layer.closeAll();
                                });
                            }else {
                               /* layer.alert(res.msg,{title:"取消默认失败"})*/
                                layer.msg('取消默认失败', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                });
                            }
                        }
                    })
                });
            } else if(obj.event === 'moren'){//设置为默认
                layer.confirm('确认置为默认么', function(){
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/bank_account/default_set',
                        data: {id: data.id, access_token: layui.data(layui.setter.tableName).access_token},
                        error: function(err){layer.alert(err),{title:"请求失败"}},
                        success: function(res){
                            if(res.code == 0){
                               /* layer.alert(res.msg,{title:"设置默认成功"},function(index){
                                    //layer.close(index);
                                    //table.reload('bankList');
                                    table.reload('tableReload');
                                    layer.closeAll();
                                });*/
                                layer.msg('设置默认成功', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                }, function(){
                                    table.reload('tableReload');
                                    layer.closeAll();
                                });
                            }else {
                               /* layer.alert(res.msg,{title:"设置默认失败"})*/
                                layer.msg('设置默认成功', {
                                    offset: '15px'
                                    ,icon: 1
                                    ,time: 1000
                                });
                            }
                        }
                    })
                });
            }
        });    

        table.render({
            elem: '#bankList'
            ,url: layui.setter.baseUrl + '/bank_account/list'
            ,where: {
                access_token: layui.data(layui.setter.tableName).access_token
            }
            ,id: 'tableReload'
            ,cols: [[
                {field:'id', title: '银行账号ID', sort: true }
                ,{field: 'accountType',title: '账号类型', templet: '<div>{{ d.accountType == 1?"银行账号":d.accountType == 2?"支付宝账号":"微信账号" }}</div>'}
                ,{field: 'accountNo',title: '账号信息'}
                ,{field: 'isDefault',title: '是否默认',templet: '<div>{{d.isDefault == 1?"默认":"不默认"}}</div>'}
                ,{field:'createTime',  title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime) }}</div>' }
                ,{field:'edite',width: 250, title: '操作', templet: tpTitle }
            ]]
            ,page: true
            ,skin: 'line'
        });


        var $ =layui.$,active = {

            add: function (othis) {
                admin.popup({
                    title: '新建银行账户'
                    , area: ['535px', '325px']
                    , id: 'LAY-popup-content-add'
                    , success: function (layero, index) {
                        view(this.id).render('sett/bank/create_bank').done(function () {
                            form.render(null, 'layuiadmin-app-form-list');

                            //监听提交
                            form.on('submit(btnJS)', function(data){
                                // 在这个回调函数里面写ajax提交
                                admin.req({
                                    type: 'post',
                                    url: layui.setter.baseUrl + '/bank_account/add',
                                    data: data.field,
                                    success: function(res){
                                        if(res.code == 0){
                                            /*layer.alert(res.msg,{title: "保存成功"},function(index){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            })*/
                                            layer.msg('保存成功', {
                                                offset: '15px'
                                                ,icon: 1
                                                ,time: 1000
                                            }, function(){
                                                table.reload('tableReload');
                                                layer.closeAll();
                                            });
                                        }else {
                                            /*layer.alert(res.msg,{title: '保存失败'})*/
                                            layer.msg('保存失败', {
                                                offset: '15px'
                                                ,icon: 1
                                                ,time: 1000
                                            });
                                        }
                                    }
                                })
                                return false;//阻止跳转
                            });
                            form.render();

                        });
                    }
                });
            }


        }


        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>